package com.huiyuanai.filter;

import com.huiyuanai.dao.User;
import com.huiyuanai.util.Constans;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: liwan
 * @Description: 过滤是否登录
 * @Date: create in 2021/10/4
 */
public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        //拿到我们需要的request和response
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        //获得session里面的数据
        User user = (User) request.getSession().getAttribute(Constans.USER_SESSION);
        //判断是否登录了，来进行跳转
        if (user == null) {
            response.sendRedirect("/error.jsp");
        }

        filterChain.doFilter(req, resp);
    }

    @Override
    public void destroy() {

    }
}
